<?php
class classOrdersList extends phpajax{
    function input(){
        aread('od_ShowDetailByOrder');
    }
    
    function loading(){}
    
    function main(){
        $UsersService = new classUsersService();
        if($UsersService->bIsLogged())
        aprint('middle_container',$this->getOrdersList($UsersService->iGetIdOfLoggedUser()));
        
        if(
            $this->od_ShowDetailByOrder!='' and 
            is_numeric($this->od_ShowDetailByOrder) and
            $this->od_ShowDetailByOrder > 0
        ){
            $_Window = new classWindow();
            aprint('flow_container2',$_Window->ShowByAJAX('flow_container2',self::getOrdersDetail($this->od_ShowDetailByOrder)));
        }
    }
    
    private function getOrdersDetail($iOrderID){
        $_Follow = new classFollow();
        $_PriceExport = new classPriceExport();
        
        $sql_result = mysql_query('
            SELECT * 
            FROM order_heads
            WHERE ID='.$iOrderID.'
        ');
        $sql_obj = mysql_fetch_object($sql_result);
        $sConfirmState  = '';
        $sReadState     = '';
        if($sql_obj->OH_Confirm == 'C') $sConfirmState = _i18n_order_CONFIRMED_;
        if($sql_obj->OH_Unread  == 'R') $sReadState = _i18n_order_READY_TO_PROCESSING_;
        if($sql_obj->NO == '') $OrderNO = $iOrderID;
        else $OrderNO = date('Y',strtotime($sql_obj->Order_Create)).'/'.$sql_obj->NO;
        $result = '
            <input type="hidden" name="od_ShowDetailByOrder" id="od_ShowDetailByOrder" value="">
            
            '._i18n_order_ORDERS_NUMBER_.': <b>'.$OrderNO.'</b>,
            '._i18n_order_CREATED_.' <b>'.date(_CLIENT_LOCAL_DATE_FORMAT_,strtotime($sql_obj->Order_Create)).'</b>
            <br>
            '._i18n_order_ORDERS_STATE_.': <b>'.$sReadState.' '.$sConfirmState.'</b>
            <br>
            <br>
            <table style="font-size:10px; width:100%;">
            <tr>
              <td>
                <b>'._i18n_product_Name_.'</b>
              </td>
            <td>
               <b>'._i18n_product_Code_.'</b>
            </td>
            <td>
              <b>'._i18n_product_UNIT_.'</b>
            </td>
            <td>
              <b>'._i18n_product_PRICE_FOR_UNIT_.'</b>
            </td>
            <td>
              <b>'._i18n_product_COUNT_.'</b>
            </td>
        ';
        if(_VAT_=='YES')
            $result .= '
                <td>
                    <b>'._i18n_product_VAT_.'</b>
                </td>
                <td>
                    <b>'._i18n_product_PRICE_WITH_VAT_.'</b>
                </td>
                <td>
                    <b>'._i18n_product_SUM_.'</b>
                </td>
            ';
       else
            $result .= '
                <td>
                    <b>'._i18n_product_SUM_.'</b>
                </td>
            ';
        $result .= '
                <td>
                    <b>'._i18n_order_Ready_.'</b>
                </td>
            ';
        
        $sql_result = mysql_query('
            SELECT * 
            FROM order_products 
            WHERE OH_ID='.$iOrderID.'
            ORDER BY OP_Name
        ');
        
        $totalSumm =0;
        
        while ($sql_obj = mysql_fetch_object($sql_result)){            
            $result .= '
                <tr
                  onmouseover="this.style.textDecoration=\'underline\';"
                  onmouseout="this.style.textDecoration=\'none\';" 
                >
                  <td
                    '.$_Follow->getJS(-1*$sql_obj->OP_ID).'
                  >
                    '.$sql_obj->OP_Name.'
                  </td>
                <td>
                   '.$sql_obj->OP_Code.'
                </td>
                <td>
                  '.$sql_obj->OP_UnitType.'
                </td>
                <td>
                  '.$_PriceExport->RoundIt_Euro($sql_obj->OP_Price).'
                </td>
                <td>
                  '.$sql_obj->OP_Count.'
                </td>
            ';
            if(_VAT_=='YES')
                $result .= '
                    <td>
                        '.$sql_obj->OP_DPH.'
                    </td>
                    <td>
                        '.$_PriceExport->RoundIt_Euro($sql_obj->OP_Price*(($sql_obj->OP_DPH/100)+1)).'
                    </td>
                    <td>
                        '.$_PriceExport->RoundIt_Euro($sql_obj->OP_Price*(($sql_obj->OP_DPH/100)+1)*$sql_obj->OP_Count).'
                    </td>
                ';
            else
                $result .= '
                    <td>
                         '.$_PriceExport->RoundIt_Euro($sql_obj->OP_Price*$sql_obj->OP_Count).'
                    </td>
                ';

            if($sql_obj->OP_Ready=='1') $OP_Ready = _i18n_order_YES_; else $OP_Ready = _i18n_order_NO_;
            $result .= '
                <td>
                    <b>'.$OP_Ready.'</b>
                </td>
            ';
            
            if(_VAT_=='YES')
                $totalSumm += $sql_obj->OP_Price*(($sql_obj->OP_DPH/100)+1)*$sql_obj->OP_Count;
            else
                $totalSumm += $sql_obj->OP_Price*$sql_obj->OP_Count;
            }
        if(_VAT_=='YES'){
            $resultSumm = $_PriceExport->RoundIt_Euro($totalSumm).' '._CURRENCY1_.' '._i18n_product_WITH_VAT_;
            $resultSumm.= '<br>'.$_PriceExport->RoundIt_SKK($totalSumm*30.126).' '._CURRENCY2_.' '._i18n_product_WITH_VAT_;
        }
        else{
            $resultSumm = $_PriceExport->RoundIt_Euro($totalSumm).' '._CURRENCY1_;
            $resultSumm.= '<br>'.$_PriceExport->RoundIt_SKK($totalSumm*30.126).' '._CURRENCY2_;
        }
        $result .= '
              </tr>
            </table>
            <br>
            <br>
            <b>'.$resultSumm.'</b>
            <br>
            <br>
            <a 
              href="admin/order_management_print.php?ID='.$iOrderID.'"  target="_blank"
            >'._i18n_order_PRINT_ORDER_.'</a>
            <br>
            <br>
        ';
        
        return $result;
    }
    
    private function getOrdersList($iUserID){
        $_Follow = new classFollow();
        $sql_result = mysql_query('SELECT * FROM order_heads WHERE OH_UserID='.$iUserID.' ORDER BY ID DESC');
        $result = '
            <input type="hidden" name="od_ShowDetailByOrder" id="od_ShowDetailByOrder" value="">
            <table style="font-size:10px;">
            <tr>
              <td>
                <b>'._i18n_order_NUMBER_.'</b>
              </td>
            <td>
               <b>'._i18n_order_DATE_.'</b>
            </td>
            <td>
              <b>'._i18n_order_STATE_.'</b>
            </td>
          </tr>
        ';
        while ($sql_obj = mysql_fetch_object($sql_result)){
            $sConfirmState  = '';
            $sReadState     = '';
            if($sql_obj->OH_Confirm == 'C')
                $sConfirmState = _i18n_order_CONFIRMED_;
            if($sql_obj->OH_Unread  == 'R')
                $sReadState = _i18n_order_READY_TO_PROCESSING_;
            if($sql_obj->NO == '') $OrderNO = $sql_obj->ID;
            else $OrderNO = date('Y',strtotime($sql_obj->Order_Create)).'/'.$sql_obj->NO;
            $result .= '
                <tr>
                  <td>
                    <a href="'._URL_INACTIVE_ADDR_.'"
                      '.$_Follow->getJS(_i18n_order_SHOW_ORDER_).'
                      onclick="
                        '.$_Follow->getClosingJS().'
                        document.getElementById(\'od_ShowDetailByOrder\').value = \''.$sql_obj->ID.'\';
                        classOrdersList();
                      "
                    >
                      '.$OrderNO.'
                    </a>
                  </td>
                <td>
                   '.date(_CLIENT_LOCAL_DATE_FORMAT_,strtotime($sql_obj->Order_Create)).'
                </td>
                <td>
                  '.$sReadState.' '.$sConfirmState.'
                </td>
              </tr>
            ';
        }
        $result .= '</table>';
        
        return $result;
    }
}
?>
